package com.example.SpringBoot_demo.controller;

import com.example.SpringBoot_demo.bean.User;
import com.example.SpringBoot_demo.service.IUserService;
import com.example.SpringBoot_demo.service.ILogService;
import com.example.SpringBoot_demo.util.MD5Util;
import com.example.SpringBoot_demo.util.RandomUserId;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired(required = false)
    private IUserService userService;
    @Autowired
    private ILogService ILogService;

    @RequestMapping("/viewUser")
    public String getAllUser(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,5);
        List<User> list = userService.viewUser();
        PageInfo<User> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        System.out.println();
        return "viewUser";
    }

    @RequestMapping("/viewAdmin")
    public String getAdmin(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,5);
        List<User> list = userService.viewAdmin();
        PageInfo<User> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        System.out.println();
        return "viewAdmin";
    }
    @GetMapping("deleteUser")
    public String deleteUser(String userid,HttpServletRequest request){
        userService.deleteUser(userid);
        ILogService.createLogs("删除用户:"+userid);//删除记录
        request.setAttribute("pageInfo.list",userService.viewUser());
        return "redirect:viewUser";
    }

    @RequestMapping("add")
    public String add(){
        return "add";
    }

    @RequestMapping(value = "addUser",method = RequestMethod.POST)
    public String addUser(Model model, User user, HttpServletRequest request){
       user.setUserid(RandomUserId.randomId());
       String password=user.getPassword();
       user.setPassword(MD5Util.encode(password));
        userService.addUser(user);
        ILogService.createLogs("添加用户:"+user.getUserid());//添加用户记录
        model.addAttribute("pageInfo.list",user);
        request.setAttribute("pageInfo.list",userService.viewUser());
        return "redirect:viewUser";
    }

//修改user数
    @GetMapping("/update")
    public String update(Model model, String userid) {
        User user = userService.getUserById(userid);
        ILogService.createLogs("修改用户:"+userid);//修改用户记录
        model.addAttribute("users", user);
        return "/update";
    }

    @RequestMapping(value = "updateUser",method = RequestMethod.POST)
    public String updateUser(User user, HttpServletRequest request) {
        userService.updateUser(user);
        request.setAttribute("users",user);
        ILogService.createLogs("修改用户1:"+user.getUserid());//修改用户记录
        request.setAttribute("users",userService.viewUser());
        return "redirect:viewUser";
    }
    @GetMapping("getUserById")
    public String getUserById(Model model, HttpServletRequest request,String userid) {
        User user = userService.getUserById(userid);
        model.addAttribute("pageInfo.list", user);
        request.setAttribute("pageInfo.list",user);
        return "/find";
    }

    @GetMapping("getUser")
    public String getUser(Model model, HttpServletRequest request,String userid,String username){
        List<User> user = userService.getUser(userid,username);
        model.addAttribute("users", user);
        request.setAttribute("pageInfo.list",user);
        return "/find";
    }


}
